$(function(){
	//新增角色
	layui.use(['element', 'layer', 'form'], function() {
		var element = layui.element(),
			layer = layui.layer,
			form = layui.form();
		
		
		var setting = {
			check: {
				enable: true
			},
			data: {
				key: {
					title: "",
					name: 'authName'
				},
				simpleData: {
					enable: true,
					idKey: 'id',
					pIdKey: 'parentId'
				}
			}									
		};
		var ztree;
		var role_methods = {
			add: function(){
				$("input[name='roleName']").val("");
				$("textarea[name='roleDesc']").val("");
				this.getMenuTree('新增角色');
			},
			update: function () {
				var roleId = 16;//获取要修改的id值
				$("input[name='roleName']").val("22");//获取角色名称、角色描述，赋值给input，textarea
				$("textarea[name='roleDesc']").val("22");
				if(roleId == (null || undefined)){
					return ;
				}
				
				this.getMenuTree('修改角色', roleId);
			},
			del: function (content, roleIds) {
				if(roleIds == (null || undefined)){
					return ;
				}
				layer.confirm(content, function(){
					$.ajax({
						type: "POST",
					    url: "http://sailorw1.6655.la:29508/sz-core/sys/sysrole/delete",
					    data: {
					    	ids: roleIds
					    },
					    dataType: 'json',
					    success: function(res){
							if(res.code == 0){
								layer.msg('操作成功');
							}else{
								layer.alert(res.msg);
							}
						}
					});
				});
			},
			getRole: function(roleId){
	    		$.ajax({
	    			type:"get",
	    			url:"http://sailorw1.6655.la:29508/sz-core/sys/sysrole/info/"+roleId,
	    			dataType: 'json',
	    			success: function(res){
	    				console.log(res);
		    			var menuIds = res.sysRole.authPkList;
		    			console.log(menuIds);
		    			for(var i=0; i<menuIds.length; i++) {
		    				var node = ztree.getNodeByParam("id", menuIds[i]);
		    				ztree.checkNode(node, true, false);
		    			}
	    			}
	    		});
			},
			saveOrUpdate: function (roleId) {
				$('#role-submit').trigger('click');//触发submit事件
				//获取选择的菜单
				var authPkListStr = this.onCheck();
				form.on('submit(formRole)', function(data){
					console.log(data.field);
					data.field.authPkListStr = authPkListStr;
					var datas = data.field;
					console.log(datas);
				    var url = roleId == (null || undefined) ? "http://sailorw1.6655.la:29508/sz-core/sys/sysrole/save" : "http://sailorw1.6655.la:29508/sz-core/sys/sysrole/update";
					$.ajax({
						
						type: "POST",
					    url: url,
					    data: datas,
					    dataType: 'json',
					    success: function(res){
					    	console.log(res);
					    	if(res.code == 0){
					    		layer.closeAll();
								layer.msg('操作成功');
							}else{
								layer.alert(res.msg);
							}
						}
					});
				});
				
				
			},
			getMenuTree: function(title, roleId) {
				var that = this;
				//加载菜单树
				$.ajax({
					url: 'http://sailorw1.6655.la:29508/sz-core/sys/sysauth/perms',
					type: "get",
					dataType: "json",
					beforeSend: function() {
						layer.load(0);
					},
					success: function(res) {
						//处理事件
						ztree = $.fn.zTree.init($("#role-tree"), setting, res.authList);
						//ztree.expandAll(true);//展开所有节点
						if(roleId != (null || undefined)){
							that.getRole(roleId);
						}
						layer.closeAll();
						layer.open({
						    type: 1,
						    title: title,
						    area: ['420px', '460px'],
						    shade: .08,
						    btn: ['确定', '取消'],
						    content: $('#role-ztree-box'),
						    yes: function(){
						    	that.saveOrUpdate();
						    }
						});
					}
				});
		    },
		    onCheck: function(){
		    	var treeObj=$.fn.zTree.getZTreeObj("role-tree"),
			    nodes=treeObj.getCheckedNodes(true),
			    authPkListStr=[];
			    for(var i=0;i<nodes.length;i++){
			        authPkListStr.push(nodes[i].id);
			        //alert(nodes[i].id); //获取选中节点的值
				}
			    return authPkListStr.join(',');
		    }
		}
		
		
		
		$("#role-newadd").on('click', function(){//点击新增按钮
			role_methods.add();
		})
		
		$(document).on('click', '.role-modify', function(){//点击修改按钮
			role_methods.update();
		})
		
		$("#role-deletes").on('click', function(){//点击删除按钮，删除多条选中数据
			var roleId = [1,2];//获取选中数据的id字符串
			var content = '确定要删除当前记录？';
			role_methods.del(content, roleId);
		})
		
		$(document).on('click', '.role-delete', function(){//点击删除按钮，删除单条当前数据
			var roleId = [6];//获取当前数据的id值
			var content = '确定要删除选中的记录？';
			role_methods.del(content, roleId);
		})
	})
});

